﻿using System;
using System.Configuration;


namespace Petroleum.Tool
{
    public class BaseTool
    {
        /// <summary>
        /// 从app.config中获取ServerUrl
        /// </summary>
        /// <returns></returns>
        public static String GetAppConfig(string key)
        {

            //获取Configuration对象
            Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            //根据Key读取<add>元素的Value
            string server = config.AppSettings.Settings[key].Value == null ? "null" : config.AppSettings.Settings[key].Value;
            ////写入<add>元素的Value
            //config.AppSettings.Settings["name"].Value = "fx163";
            ////增加<add>元素
            //config.AppSettings.Settings.Add("url", "http://www.fx163.net");
            ////删除<add>元素
            //config.AppSettings.Settings.Remove("name");
            ////一定要记得保存，写不带参数的config.Save()也可以
            //config.Save(ConfigurationSaveMode.Modified);
            ////刷新，否则程序读取的还是之前的值（可能已装入内存）
            //System.Configuration.ConfigurationManager.RefreshSection("appSettings");
            return server;
        }
     
        /// <summary>
        /// 修改app.config的自定义属性
        /// </summary>
        /// <returns></returns>
        public static void UpdateAppConfig(string key, string newValue)
        {

            //获取Configuration对象
            Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            //根据Key读取<add>元素的Value
            //string server = config.AppSettings.Settings[key].Value == null ? "null" : config.AppSettings.Settings[key].Value;
            ////写入<add>元素的Value
            config.AppSettings.Settings[key].Value = newValue;
            ////增加<add>元素
            //config.AppSettings.Settings.Add("url", "http://www.fx163.net");
            ////删除<add>元素
            //config.AppSettings.Settings.Remove("name");
            ////一定要记得保存，写不带参数的config.Save()也可以
            config.Save(ConfigurationSaveMode.Modified);
            ////刷新，否则程序读取的还是之前的值（可能已装入内存）
            System.Configuration.ConfigurationManager.RefreshSection("appSettings");


        }
        /// <summary>
        /// 修改app.config的自定义属性-重载接收Int类型，用于枚举取值
        /// </summary>
        /// <returns></returns>
        public static void UpdateAppConfig(string key, int newValue)
        {

            //获取Configuration对象
            Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            //根据Key读取<add>元素的Value
            //string server = config.AppSettings.Settings[key].Value == null ? "null" : config.AppSettings.Settings[key].Value;
            ////写入<add>元素的Value
            config.AppSettings.Settings[key].Value = newValue.ToString();
            ////增加<add>元素
            //config.AppSettings.Settings.Add("url", "http://www.fx163.net");
            ////删除<add>元素
            //config.AppSettings.Settings.Remove("name");
            ////一定要记得保存，写不带参数的config.Save()也可以
            config.Save(ConfigurationSaveMode.Modified);
            ////刷新，否则程序读取的还是之前的值（可能已装入内存）
            System.Configuration.ConfigurationManager.RefreshSection("appSettings");


        }
    }
}
